package door.service.impl;

import app.nbdevices.utils.ConvertUtil;
import app.nbdevices.utils.HttpsUtil;
import app.nbdevices.utils.StackOperator;
import door.entity.DoorCmdData;
import door.entity.DoorCycleReport;
import door.faultHandler.DoorFaultHandler;
import door.modle.codecModle.DoorCmd;
import door.modle.codecModle.DoorReport;
import door.modle.revModle.RevDoorCmdData;
import door.modle.revModle.RevDoorReportData;
import door.service.IDoorCmdDataService;
import door.service.IDoorCycleReportService;
import door.service.IDoorService;
import door.utils.DoorCmdGenerator;
import door.utils.DoorDecode;
import door.utils.DoorExplain;
import door.utils.SendCmd;
import org.apache.commons.codec.EncoderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;



@Service("DoorService")
public class DoorServiceImpl implements IDoorService {
    @Autowired
    @Qualifier("doorCmdDataService")
    IDoorCmdDataService svcDoorCmdReport;

    @Autowired
    @Qualifier("doorCycleReportService")
    IDoorCycleReportService svcDoorCycleData;

    @Autowired
    @Qualifier("HttpsUtil")
    HttpsUtil httpsUtil;

    @Autowired
    DoorFaultHandler doorFaultHandler;


    @Override
    public void sendDoorOperCmd(String internetDevId,int islock,int isswitch) throws Exception {
        String cmdData=DoorCmdGenerator.doorOperator(islock,isswitch);
       // StackOperator.addLock(addrId,4,cmdData);
        SendCmd.doorOper(internetDevId,"T00001",cmdData,httpsUtil);
     //   SendCmd.doorOper("f6a7fe50-6e38-426f-aec7-80b2f309d3df","T00001",cmdData,httpsUtil);
    }

    @Override
    public void revReportHandle(String deviceId, RevDoorReportData revDoorReportData) {
        DoorReport doorReport= DoorDecode.doorReportDataDecode(revDoorReportData);
        DoorCycleReport doorCycleReport= DoorExplain.exDoorReport(deviceId,doorReport);
        //System.out.println(doorCycleReport);
        doorFaultHandler.mainHandler(doorCycleReport);
        svcDoorCycleData.save(doorCycleReport);
    }

    @Override
    public void revCmdHandle(String deviceId, RevDoorCmdData revDoorCmdData) {
        DoorCmd doorCmd=DoorDecode.doorCmdDecode(revDoorCmdData);
        String addrId= new String(ConvertUtil.getBase64Codec().decode(revDoorCmdData.getAddrId()));
        StackOperator.DoorunlockInfo u=StackOperator.getUnlockInfo(addrId);
        if(u!=null){
            DoorCmdData doorCmdData=new DoorCmdData();
            doorCmdData.setCmdData(u.getCmdData());
            doorCmdData.setInternetDeviceId(deviceId);
            doorCmdData.setCmdTime(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
            doorCmdData.setCmdStatus(revDoorCmdData.getIsSuc());
            System.out.println(doorCmdData);
        }
    }

    @Override
    public void sendSwitch(String internetDevId) throws Exception {
        String cmd1=DoorCmdGenerator.doorOperator(0,1);
        String cmd2=DoorCmdGenerator.doorOperator(0,0);
        //先开后关
        SendCmd.doorOper(internetDevId,"T00063",cmd1,httpsUtil);
        new Thread(){
            public void run(){
                //子线程去关门
                try {
                    sleep(10000l);
                    SendCmd.doorOper(internetDevId,"T00063",cmd2,httpsUtil);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}
